using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThenParameter : MonoBehaviour
{
    public ThenItem m_ThenItemPrefab;
    public GameObject m_ThenItemsParent;
    public List<ThenItem> m_ThenItems = new List<ThenItem>();
    public ConditionList m_ConditionList;
    ConditionItem m_ConditionItem;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void InitThenParameter(ConditionItem conditionItem)
    {
        foreach (var i in m_ThenItems)
        {
            Destroy(i.gameObject);
        }
        m_ThenItems.Clear();
        m_ConditionItem = conditionItem;
        var beaviours = conditionItem.m_ShipEvent.m_Events.Value;
        if (beaviours != null)
            foreach (var behaviour in beaviours)
            {
                var thenItem = AddItem(behaviour);
                thenItem.Init(behaviour);
            }
    }
    public void OnAddThenItemCliced()
    {
        if (m_ConditionItem == null)
            return;
        AddItem();
    }
    ThenItem AddItem(ShipEventBehaviours behaviours = null)
    {
        var thenItem = Instantiate(m_ThenItemPrefab);
        thenItem.gameObject.transform.SetParent(m_ThenItemsParent.transform);
        thenItem.transform.SetSiblingIndex(Mathf.Max(m_ThenItemsParent.transform.childCount - 3, 0));
        thenItem.m_ConditionList = m_ConditionList;
        if(behaviours == null)
            thenItem.Init(new ShipEventBehaviours());
        else
            thenItem.Init(behaviours);

        m_ThenItems.Add(thenItem);
        return thenItem;
    }
}
